#include "pastbin.h"

#include <httpclient.h>

Pastbin::Pastbin()
{

}

QString Pastbin::name()
{
    return "Pastbin";
}

QString Pastbin::prefix()
{
    return "http://118.178.197.102:8080/pastebin/api";
}

QStringList Pastbin::searching(const QString &text)
{
    QStringList list;
    auto url = prefix() + "/all";
    auto items = HttpClient::instance().get(url).toList();
    foreach (auto var, items) {
        auto id = var.toMap().value("id").toInt();
        auto title = var.toMap().value("title").toString();

        list.append(QString("%1: %2").arg(id).arg(title));
    }
    return list;
}

QString Pastbin::content(const QString &text)
{
    if (text.split(":").length() > 1) {
        int _id = text.split(":").at(0).toInt();

        auto url = prefix() + "/all";
        auto list = HttpClient::instance().get(url).toList();
        foreach (auto var, list) {
            auto id = var.toMap().value("id").toInt();
            if (id == _id) {
                url = prefix() + "/show/" + var.toMap().value("uuid").toString();
                return "<pre>"+HttpClient::instance().get(url).toMap().value("paste").toMap().value("content").toString()+"</pre>";
            }
        }
    }
    return "";
}

bool Pastbin::autoLoad()
{
    return true;
}



/**
curl 'http://192.168.10.75:8080/pastebin/api/all' \
  -H 'Accept: * /*' \
  -H 'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8' \
  -H 'Cache-Control: no-cache' \
  -H 'Cookie: ajs_anonymous_id=daaf7280-b385-4e5f-89ee-aac96ed78d47; _xsrf=2|3d9c743b|2ff8781f7b248a292ec82116b2b4c55b|1724062410; _pk_id.1.b277=74c4b4f01059c692.1724122600.' \
  -H 'Pragma: no-cache' \
  -H 'Proxy-Connection: keep-alive' \
  -H 'Referer: http://192.168.10.75:8080/pastebin/' \
  -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36' \
  -H 'X-Requested-With: XMLHttpRequest' \
  --insecure
*/
